package com.hmall.trade.listener;

import com.hmall.api.client.TradeClient;
import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * ClassName: PayStatusListener
 * Package:
 * Description
 * 声明队列和交换机  AMQP依赖，包含RabbitMQ
 *
 * @Author xw
 * @Create 2023/12/14 20:34
 */
@Component
public class PayStatusListener {


    @Autowired
    private IOrderService iOrderService;

    // 修改订单状态
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "mark.order.pay.queue", declare = "true"),
            exchange = @Exchange(name = "pay.topic", type = ExchangeTypes.TOPIC),
            key = "pay.success"
    ))
    public void markOrderPaySuccess(Long payOrderNo) {

       /* // 查询订单
        Order byId = iOrderService.getById(payOrderNo);
        // 2。判断订单状态是否为未支付
        if (byId == null || byId.getStatus() != 1){
            // 订单不存在，或者状态异常 dwa
            return;
        }
        // ，如果未支付，标记订单状态为已支付
        iOrderService.markOrderPaySuccess(payOrderNo);*/


        // updata order set status = 2 where id = ?

        iOrderService.lambdaUpdate()
                .set(Order::getStatus, 2)
                .set(Order::getPayTime, LocalDateTime.now())
                .eq(Order::getId, payOrderNo)
                .eq(Order::getStatus, 1)
                .update();

    }
}
